set(SOURCES
  hypertable.c
  init.c
  license.c
  reorder.c
  telemetry.c
  partialize_finalize.c
  planner.c
)

if (PG_VERSION_SUPPORTS_MULTINODE)
  list(APPEND SOURCES
       async_append.c
       chunk.c
       deparse.c
       data_node.c
       data_node_dispatch.c
       chunk_api.c
       process_utility.c
       dist_util.c)
  
  # Add test source code in Debug builds
  if (CMAKE_BUILD_TYPE MATCHES Debug)
    set(TS_DEBUG 1)
    set(DEBUG 1)
    list(APPEND SOURCES
        debug.c)
  endif (CMAKE_BUILD_TYPE MATCHES Debug)
  
endif()

set(TSL_LIBRARY_NAME ${PROJECT_NAME}-tsl)

include(build-defs.cmake)

if (CMAKE_BUILD_TYPE MATCHES Debug)
add_library(${TSL_LIBRARY_NAME} MODULE ${SOURCES} $<TARGET_OBJECTS:${TSL_TESTS_LIB_NAME}>)
else ()
add_library(${TSL_LIBRARY_NAME} MODULE ${SOURCES})
endif ()

set_target_properties(${TSL_LIBRARY_NAME} PROPERTIES
  OUTPUT_NAME ${TSL_LIBRARY_NAME}-${PROJECT_VERSION_MOD}
  PREFIX "")

target_include_directories(${TSL_LIBRARY_NAME} PRIVATE ${PG_INCLUDEDIR})
if (USE_OPENSSL)
  target_include_directories(${TSL_LIBRARY_NAME} PRIVATE ${OPENSSL_INCLUDE_DIR})
endif (USE_OPENSSL)

target_compile_definitions(${TSL_LIBRARY_NAME} PUBLIC TS_TSL)
target_compile_definitions(${TSL_LIBRARY_NAME} PUBLIC TS_SUBMODULE)

if (PG_VERSION_SUPPORTS_MULTINODE)
  if (WIN32)
    target_link_libraries(${TSL_LIBRARY_NAME} ${PG_LIBDIR}/libpq.lib)
  else ()
    target_link_libraries(${TSL_LIBRARY_NAME} pq)
  endif ()
endif(PG_VERSION_SUPPORTS_MULTINODE)

install(
  TARGETS ${TSL_LIBRARY_NAME}
  DESTINATION ${PG_PKGLIBDIR})

# if (WIN32)
#   target_link_libraries(${PROJECT_NAME} ${PROJECT_NAME}-${PROJECT_VERSION_MOD}.lib)
# endif(WIN32)

add_subdirectory(bgw_policy)
add_subdirectory(compression)
add_subdirectory(continuous_aggs)
add_subdirectory(nodes)

if (PG_VERSION_SUPPORTS_MULTINODE)
  add_subdirectory(fdw)
  add_subdirectory(remote)
endif ()
